/*
** rt.c for RayTracer in /home/jeanma_d//Damien/Igraph/RayTracer
** 
** Made by damien jeanmaire
** Login   <jeanma_d@epitech.net>
** 
** Started on  Thu Mar 29 10:51:24 2012 damien jeanmaire
** Last update Thu Apr 26 17:04:26 2012 damien jeanmaire
*/

#include	"rt.h"

void		calc_vect(t_scene *sc, int x, int y)
{
sc->vect.x = -(sc->size.x / (2 * tan(sc->eye.fov / 2)));
sc->vect.y = (sc->size.x / 2) - x;
sc->vect.z = (sc->size.y / 2) - y;
sc->vect = rotate(&(sc->vect), &(sc->eye.rot));
}

static void	disp_loading_border(t_mlx *ptr, t_scene *sc)
{
  int		x;
  int		y;

  x = (sc->size.x / 4) - 1;
  while (x <= 3 * (sc->size.x / 4) + 1)
    {
      if (x == (sc->size.x / 4) - 1 || x == 3 * (sc->size.x / 4) + 1)
	{
	  y = (sc->size.y / 2) - 9;
	  while (y <= (sc->size.y / 2) + 8)
	    mlx_pixel_put(ptr->mlx, ptr->win, x, y++, 0xFFFFFF);
	}
      else
	{
	  mlx_pixel_put(ptr->mlx, ptr->win, x, (sc->size.y / 2) - 9, 0xFFFFFF);
	  mlx_pixel_put(ptr->mlx, ptr->win, x, (sc->size.y / 2) + 8, 0xFFFFFF);
	}
      x++;
    }
}

static void	disp_loading(int percent, t_mlx *ptr, t_scene *sc)
{
  int		x;
  int		y;

  x = (sc->size.x / 4) + percent * (sc->size.x / 200);
  while (x < ((sc->size.x / 4) + ((percent + 1) * sc->size.x) / 200))
    {
      y = (sc->size.y / 2) - 8;
      while (y < (sc->size.y / 2) + 8)
	{
	  mlx_pixel_put(ptr->mlx, ptr->win, x, y, 0xAAFFC9);
	  ++y;
	}
      ++x;
    }
}

void		fill_image(t_mlx *ptr, t_img *img, t_scene *sc)
{
  int		x;
  int		y;
  int		percent;
  unsigned int	color;

  percent = -1;
  y = 0;
  disp_loading_border(ptr);
  while (y < sc->size.y)
    {
      x = 0;
      while (x < sc->size.x)
	{
	  if ((y * 100) / sc->size.y != percent)
	    {
	      percent = (y * 100) / sc->size.y;
	      disp_loading(percent, ptr);
	    }
	  calc_vect(sc, x, y);
	  color = mlx_get_color_value(ptr->mlx, calc(sc));
	  my_pixel_put_to_image(img, x, y, color);
	  x++;
	}
      y++;
    }
}
